package action.FrontAction;

import com.opensymphony.xwork2.ActionSupport;
import entity.CroCustomer;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroCustomerService;

import java.util.UUID;

/**
 * Created by xuweijie on 2017/6/9.
 */
public class ForgetPwdAction extends ActionSupport {

    @Autowired
    private CroCustomerService croCustomerService;

    private String username;
    private String password;
    private String type;
    private String successResult;

    /**
     * 显示忘记密码页面
     * @return
     */
    public String showList(){
        return SUCCESS;
    }

    /**
     * 重置密码
     * @return
     */
    public String resetPwd(){
        CroCustomer croCustomer=croCustomerService.FindCusByEmailOrPhone(username,type);
        if(croCustomer!=null){
            if((new Md5Hash(password,croCustomer.getSalt()).toString()).equals(croCustomer.getPasswd())){
                successResult="2";
            }else{
                String salt= UUID.randomUUID().toString().substring(0,8);
                croCustomer.setPasswd(new Md5Hash(password,salt).toString());
                croCustomer.setSalt(salt);
                if(croCustomerService.updateCustomer(croCustomer)){
                    successResult="0";
                }else {
                    successResult="1";
                }
            }
        }else{
            if (type.equals("1")){
                successResult="邮箱验证未通过";
            }else {
                successResult="手机号码验证未通过";
            }
        }
        return SUCCESS;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }
}
